package cn.tom.config.smsFilter;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.SpringSecurityCoreVersion;

import java.util.Collection;

public class SmsCodeAuthenticationToken extends AbstractAuthenticationToken {
    private final static long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;
    private final Object principal;

    public SmsCodeAuthenticationToken(Object principal) {
        super(null);
        this.principal = principal;
        super.setAuthenticated(false);
    }

    public SmsCodeAuthenticationToken( Object principal,Collection<? extends GrantedAuthority> authorities) {
        super(authorities);
        this.principal = principal;
        super.setAuthenticated(true);
    }

    @Override
    public Object getCredentials() {
        return null;
    }

    @Override
    public Object getPrincipal() {
        return this.principal;
    }

    @Override
    public void setAuthenticated(boolean authenticated) {
        if (authenticated && !this.isAuthenticated()){
            super.setAuthenticated(true);
        }else if (!authenticated && this.isAuthenticated()){
            throw new IllegalArgumentException(
                    "Cannot set this token to not trusted - use constructor which takes no GrantedAuthority list instead");
        }
    }

    @Override
    public void eraseCredentials() {
        super.eraseCredentials();
    }
}
